﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Lavka.Models;

namespace Lavka.Core
{
    public class OrdersHelper
    {
        public static string GenerateOrderKey(int userId)
        {
            Random r = new Random();
            int key = r.Next(100000, 999999);
            return key.ToString() + userId;
        }
        public static string GetActionPrice(Products product, LavkaDbEntities context)
        {
            if (context.Actions.Where(a => a.ProductId == product.ProductId).Count() > 0)
            {
                var productAction = context.Actions.Where(a => a.ProductId == product.ProductId).First();
                if (!string.IsNullOrEmpty(productAction.NewPrice.ToString()))
                {
                    return string.Format("{0:F}", productAction.NewPrice);
                }
            }
            return null;
        }
        public static decimal GetOrderSumPrice(int orderId, LavkaDbEntities context)
        {
            decimal sum = 0;
            var products = context.OrderedProducts.Where(op => op.OrderId == orderId);
            foreach (var orderedProduct in products)
            {
                sum += Convert.ToDecimal(ProductsHelper.GetProductPrice(orderedProduct.Products, context)) * orderedProduct.Number;
            }
            return sum;
        }
    }
}